package com.ruoyi.framework.web.service;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.parser.OrderByParser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qck
 * @desc 基础服务类
 * @data 2021/12/8 9:19
 */
public class BaseService<T> {
    public Page<T> getPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<T> page = new Page(pageDomain.getPageNum(), pageDomain.getPageSize());
        List<OrderItem> orders = new ArrayList<OrderItem>();
        OrderItem orderItem = new OrderItem();
        String orderByColumn = SqlUtil.escapeOrderBySql(pageDomain.getOrderByColumn());
        orderItem.setColumn(StringUtils.toUnderScoreCase(orderByColumn));
        orderItem.setAsc(pageDomain.getIsAsc());
        orders.add(orderItem);
        page.setOrders(orders);
        return page;
    }
}
